
<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="版号" prop="code">
        <el-input v-model="formData.code" placeholder="请输入版号" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="支数" prop="branchNum">
        <el-input v-model="formData.branchNum" placeholder="请输入支数" />
      </el-form-item>
      <el-form-item label="下单数量" prop="orderNum">
        <el-input v-model="formData.orderNum" placeholder="请输入下单数量" />
      </el-form-item>
      
      <el-input type="hidden" v-model="formData.depotCode" />
    
      <el-form-item label="仓库名称" prop="depotAddress">
        <!-- <el-input v-model="formData.depotAddress" placeholder="请输选择仓库" /> -->
      
        <el-select
          v-model="formData.depotAddress"
          placeholder="请选择仓库"
          @change = "checkDepot"
          clearable
          class="!w-240px"
        >
        <el-option
            v-for="wh in wlist"
            :key="wh.id"
            :label="wh.name"
            :value="(wh)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="编号" prop="depotAddressCode">
        <!-- <el-input v-model="formData.depotAddressCode" placeholder="请输选择编号" /> -->
        <el-select
          v-model="formData.depotAddressCode"
          placeholder="请选择仓库编号"
          clearable
          @click="checkDepotCode"
          class="!w-240px"
        >
        <el-option
            v-for="whc in whcList"
            :key="whc.code"
            :label="whc.code"
            :value="whc.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="﻿尺寸" prop="size">
        <el-input v-model="formData.size" placeholder="请输入﻿尺寸" />
      </el-form-item>
      
      <el-form-item label="下单日期" prop="orderTime">
        <el-date-picker
          v-model="formData.orderTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择下单日期"
        />
      </el-form-item>
      <el-form-item label="厂家名称" prop="factoryName">
        <el-input v-model="formData.factoryName" placeholder="请输入厂家名称" />
      </el-form-item>
      <el-form-item label="到厂日期" prop="appearTime">
        <el-date-picker
          v-model="formData.appearTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择到厂日期"
        />
      </el-form-item>
      <!-- <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序" />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="formData.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ERP_PLATE_ROLLER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="descr">
        <el-input v-model="formData.descr" placeholder="请输入描述" />
      </el-form-item>
      
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { PlateRollerApi, PlateRollerVO } from '@/api/erp/plateroller'
import { WarehouseApi, WarehouseVO } from '@/api/erp/stock/warehouse'
import { WarehouseCodeApi, WarehouseCodeVO } from '@/api/erp/stock/warehouse/code'


/** ﻿版辊 表单 */
defineOptions({ name: 'PlateRollerForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  code: undefined,
  name: undefined,
  factoryName: undefined,
  branchNum: undefined,
  orderNum: undefined,
  depotCode: undefined,
  depotAddress: undefined,
  depotAddressCode: undefined,
  size: undefined,
  orderTime: undefined,
  appearTime: undefined,
  sort: undefined,
  descr: undefined,
  status: undefined,
})
const formRules = reactive({

  code: [{ required: true, message: '版辊编码不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '名称不能为空', trigger: 'blur' } ],
  depotAddress: [{ required: true, message: '仓库不能为空', trigger: 'blur' } ],
  depotCode: [{ required: true, message: '编号不能为空', trigger: 'blur' } ],
})
const formRef = ref() // 表单 Ref

const wlist = ref<WarehouseVO[]>([]) // 仓库列表的数据
const whcList = ref<WarehouseCodeVO[]>([]) // 仓库编号 列表的数据

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  try {
    if (id) {
      formLoading.value = true
      formData.value = await PlateRollerApi.getPlateRoller(id)
    }
    const platerollerWarehouse = await WarehouseApi.getWarehousePage({type:2});
    wlist.value = platerollerWarehouse.list;
  } finally {
    formLoading.value = false
  }
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PlateRollerVO
    if (formType.value === 'create') {
      await PlateRollerApi.createPlateRoller(data)
      message.success(t('common.createSuccess'))
    } else {
      await PlateRollerApi.updatePlateRoller(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    code: undefined,
    name: undefined,
    branchNum: undefined,
    orderNum: undefined,
    depotCode: undefined,
    depotAddress: undefined,
    depotAddressCode: undefined,
    size: undefined,
    orderTime: undefined,
    appearTime: undefined,
    factoryName: undefined,
    sort: undefined,
    descr: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}

   // 检测 是否选择了仓库
const checkDepot = async (value) => {
  try{
    if(value){
      formData.value.depotCode = value.id
      formData.value.depotAddress = value.name;
      formData.value.depotAddressCode = undefined;
      const WareHouseCodeQueryParams:{warehouseId:string} = { warehouseId: value.id};
      const WareHouseCodeList:{total:number,list:WarehouseCodeVO[]} = await WarehouseCodeApi.getWarehouseCodePage(WareHouseCodeQueryParams);
      console.log(WareHouseCodeList);
      whcList.value = WareHouseCodeList.list;
    }   
  }catch(error){
    console.log(error)
  }
}

const  checkDepotCode = async () => {
  try{
    if(!formData.value.depotCode){
      message.alertWarning("请先选择仓库");
    } 
  }catch(error){
    console.log(error)
  }
}

